// pages/fumehood/index.js


const app = getApp()
let _timer = null;
let _timer1 = null;
let _timer2 = null;
let _timer3 = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    deviceId: null,
    commandLight: null,
    commandPower: null,
    commandForce: null,
    commandMode: null,
    commandDoor: null,
    startKey: "",
    endKey: "",
    duration: null,
    disable: false,
    disableWindow: false,
    downPosition: 40,
    upPosition: 30,
    windowHeightKey: null,
    startWindowHeight: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let deviceId = options.deviceId
    if (deviceId != null) {
      this.setData({
        deviceId: deviceId
      })
      app.api.getDeviceDetail(deviceId).then(res => {
        if (res.data != null) {
          this.setData({
            device: res.data
          })
          wx.setNavigationBarTitle({
            title: res.data.deviceName
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let _this = this
    _timer1 = setInterval(function () {
      if (_this.data.deviceId != null) {
        app.api.getDeviceDetail(_this.data.deviceId).then(res => {
          if (res.data != null) {
            if (_this.data.commandLight != null && _this.data.commandLight == res.data.light) {
              _this.setData({
                commandLight: null
              })
            } else if (_this.data.commandPower != null && _this.data.commandPower == res.data.power) {
              _this.setData({
                commandPower: null
              })
            } else if (_this.data.commandForce != null && _this.data.commandForce == res.data.force) {
              _this.setData({
                commandForce: null
              })
            } else if (_this.data.commandMode != null && _this.data.commandMode == res.data.workingMode) {
              _this.setData({
                commandMode: null
              })
            } else if (_this.data.commandDoor != null && _this.data.commandDoor == res.data.isAutoDoor) {
              _this.setData({
                commandDoor: null
              })
            } else if (res.data.windowHeight != null) {
              let value = parseInt(res.data.windowHeight)
              if (_this.data.windowHeightKey == 'reduce' && value < 10) {
                if (_timer2 != null) {
                  clearInterval(_timer2)
                }
                _this.setData({
                  windowHeightKey: null,
                  startWindowHeight: null,
                  downPosition: 40
                })
              } else if (_this.data.windowHeightKey == 'add' && value > 40) {
                if (_timer3 != null) {
                  clearInterval(_timer3)
                }
                _this.setData({
                  windowHeightKey: null,
                  startWindowHeight: null,
                  upPosition: 30
                })
              }
            }
            _this.setData({
              device: res.data
            })
          }
        })
      }
    }, 2500)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.data.startKey = ''
    this.data.endKey = ''
    this.data.duration = null
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
    if (_timer2 != null) {
      clearInterval(_timer2)
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
    if (_timer2 != null) {
      clearInterval(_timer2)
    }
    if (_timer3 != null) {
      clearInterval(_timer2)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handleWindowHeight(e) {
      let key = e.currentTarget.dataset.key;
      let v = this.data.device
      let val = -1;
      if (v.isRunning != "1") {
          wx.showToast({
              title: '窗高只能在设备开始时调整',
              icon: 'none'
          })
          return
      }
      if (this.data.disableWindow) {
          return
      }
      this.data.disableWindow = true
      if (v.windowHeight != null) {
          val = parseFloat(v.windowHeight);
      }
      let _this = this
      this.data.windowHeightKey = key
      this.data.startWindowHeight = v.windowHeight
      if (key == 'reduce') {
          val = 0;
          _timer2 = setInterval(function () {
              let positon = _this.data.downPosition;
        if (positon >= 65) {
          positon = 0
        } else {
          positon = positon + 1
        }
        _this.setData({
          downPosition: positon
        })
      }, 100)
    } else if (key == 'add') {
      val = 100
      _timer3 = setInterval(function () {
        let positon = _this.data.upPosition;
        if (positon <= -25) {
          positon = 70
        } else {
          positon = positon - 1
        }
        _this.setData({
          upPosition: positon
        })
      }, 100)
    }
    v.commandKey = 'windowHeight'
    v.commandValue = val + ''
    app.api.updateDevice(v).then(res => {
      if (res.code == 200) {
        this.setData({
          device: v
        })
        wx.showToast({
          title: '修改成功!',
          icon: 'success'
        })
      }
    })
    if (_timer != null) {
      clearTimeout(_timer)
    }
    _timer = setTimeout(function () {
      _this.data.disableWindow = false
    }, 1000)
  },
  updateCommand(e) {
    if (this.data.disable) {
      return
    }
    this.data.disable = true
    let _this = this
    let key = e.currentTarget.dataset.key;
    let v = this.data.device
    switch (key) {
      case 'light':
        if (v != null)
          if (v.light == null || v.light == '' || v.light == '0') {
            v.commandKey = 'light'
            v.commandValue = '1'
          } else {
            v.commandKey = 'light'
            v.commandValue = '0'
          }
        this.setData({
          commandLight: v.commandValue
        })
        setTimeout(function () {
          if (_this.data.commmandLight != null) {
            _this.setData({
              commandLight: null
            })
          }
        }, 1500)
        break;
      case 'power':
        if (v != null)
          if (v.isRunning == null || v.isRunning == '' || v.isRunning == '0') {
            v.commandKey = 'power'
            v.commandValue = '1'
          } else {
            v.isRunning = "0"
            v.commandKey = 'power'
            v.commandValue = '0'
          }
        this.setData({
          commandPower: v.commandValue
        })
        setTimeout(function () {
          if (_this.data.commandPower != null) {
            _this.setData({
              commandPower: null
            })
          }
        }, 1500)
        break;
      case 'force':
          if (v != null)
              if (v.isRunning != "1") {
                  wx.showToast({
                      title: '强排只能在设备开始时使用',
                      icon: 'none'
                  })
                  return
              }
          if (v.force == null || v.force == '' || v.force == '0') {
              v.commandKey = 'force'
              v.commandValue = '1'
          } else {
              v.commandKey = 'force'
              v.commandValue = '0'
          }
        this.setData({
          commandForce: v.commandValue
        })
        setTimeout(function () {
          if (_this.data.commandForce != null) {
            _this.setData({
              commandForce: null
            })
          }
        }, 1500)
        break;
      case 'mode':
        if (v != null)
          if (v.workingMode == null || v.workingMode == '' || v.workingMode == '0') {
            v.commandKey = 'mode'
            v.commandValue = '1'
          } else {
            v.commandKey = 'mode'
            v.commandValue = '0'
          }
        this.setData({
          commandMode: v.commandValue
        })
        setTimeout(function () {
          if (_this.data.commandMode != null) {
            _this.setData({
              commandMode: null
            })
          }
        }, 1500)
        break;
      case 'door':
        if (v != null)
          if (v.isAutoDoor == null || v.isAutoDoor == '' || v.isAutoDoor == '0') {
            v.commandKey = 'isAutoDoor'
            v.commandValue = '1'
          } else {
            v.commandKey = 'isAutoDoor'
            v.commandValue = '0'
          }
        this.setData({
          commandDoor: v.commandValue
        })
        setTimeout(function () {
          if (_this.data.commandDoor != null) {
            _this.setData({
              commandDoor: null
            })
          }
        }, 1500)
        break;
      case 'repair':
          if (v != null)
              app.api.oneKeyRepair({
                  deviceId: this.data.device.deviceId,
                  deviceName: this.data.device.deviceName
              }).then(res => {
                  wx.showToast({
                      title: '上报成功',
                  })
              })
        break;
    }
    if (key != 'repair') {
      app.api.updateDevice(v).then(res => {
        if (res.code == 200) {
          this.setData({
            device: v
          })
        }
      })
    }
    if (_timer != null) {
      clearTimeout(_timer)
    }
    _timer = setTimeout(function () {
      _this.data.disable = false
    }, 1000)
  },
})